#include "endpointrule.h"

#include "../documents/data/graphicdata.h"
#include "../documents/swpprocessdocument.h"
#include "../editors/items/graphicitemcommon.h"

#include <gdmodelquerymaker.h>

using namespace core;
using namespace swp;

EndPointRule::EndPointRule() : IValidatorRule()
{

}

EndPointRule::~EndPointRule()
{

}

bool EndPointRule::isProjectWide() const
{
    return false;
}

bool EndPointRule::validate(
    const QSharedPointer<IContainerDocument>& document,
    QAction*& reportClickAction, QString& reportText) const
{
    return false;
}

bool EndPointRule::validate(const QSharedPointer<IDocument>& document,
    QAction*& reportClickAction, QString& reportText) const
{
    bool ret = false;

    GraphicData* data = dynamic_cast<GraphicData* >(document->data());

    Q_ASSERT(data != 0); // GraphicData expected

    GDModelQueryMaker queryMaker(*data);

    const int epItemsCount = queryMaker.getItems(SWP_END_POINT_ITEM).size();

    ret = epItemsCount == 1;

    reportText = epItemsCount == 0 ? QObject::tr("Missing End point") :
        QObject::tr("Activity diagram should contain only one End point");

    return ret;
}
